﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class RoleBO : BaseBO
    {
        #region Field list from database
        public const string FIELD_ID = "RoleId";
        public const string FIELD_NAME = "RoleName";
        public const string FIELD_CODE = "RoleCode";
        public const string FIELD_DESCRIPTION = "RoleDescription";
        public const string FIELD_APP_ID = "AppID";
        public const string FIELD_IS_ACTIVE = "IsActive";
        #endregion
        public const string FIELD_RECORD_COUNT = "RecordCount";

        private String _connectionString;

        public RoleBO(string connString)
        {
            this._connectionString = connString;

        }
        public RoleBO()
        {
            this._connectionString = Common.ConnectionUtil.DHQG_ADMIN_CONNECTION_STRING;

        }

        public RoleDAO _RoleDAO;

        public RoleDAO CreateSQLDAO()
        {
            return new RoleDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<RoleModel> GetDynamicListPaging(RoleModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},
                    {RoleModel.FIELD_CODE,FIELD_CODE},  
                    {RoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION}, 
                    {RoleModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {RoleModel.FIELD_APP_ID, FIELD_APP_ID},
                    {RoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };
                return _RoleDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public RoleModel GetRoleById(int? id)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},
                    {RoleModel.FIELD_CODE,FIELD_CODE},  
                    {RoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION},  
                    {RoleModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {RoleModel.FIELD_APP_ID, FIELD_APP_ID},
                    {RoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _RoleDAO.GetById(id, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool createOrUpdateRole(RoleModel model)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                return _RoleDAO.createOrUpdateRole(model);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteRole(int[] ID)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                return _RoleDAO.deleteRole(ID);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<RoleModel> GetAll()
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME}                    
                };
                return _RoleDAO.GetAll(mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public IEnumerable<RoleModel> GetAllRoleByUserIdAndAppCode(int userId, string appCode)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},
                    {RoleModel.FIELD_CODE,FIELD_CODE},  
                    {RoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION},
                    {RoleModel.FIELD_APP_ID, FIELD_APP_ID},
                    {RoleModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE}
                };

                return _RoleDAO.GetAllByUserIdAndAppCode(userId,appCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public IEnumerable<RoleModel> GetAllByAppId(int appid)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},
                    {RoleModel.FIELD_CODE,FIELD_CODE},  
                    {RoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION},
                    {RoleModel.FIELD_APP_ID, FIELD_APP_ID},
                    {RoleModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE}
                };

                return _RoleDAO.GetAllByAppId(appid, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<RoleModel> GetRoleNameByUserId(int userId)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},                                     
                };
                return _RoleDAO.GetRoleNameByUserId(userId,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<RoleModel> GetListRoleByApp(int AppId)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},
                    {RoleModel.FIELD_CODE,FIELD_CODE}
                };
                return _RoleDAO.GetListRoleByApp(AppId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<RoleModel> getRoleNameByGroupId(int? groupId)
        {
            try
            {
                _RoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {RoleModel.FIELD_ID,FIELD_ID},
                    {RoleModel.FIELD_NAME,FIELD_NAME},                                     
                };
                return _RoleDAO.getRoleNameByGroupId(groupId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}